home *** CD-ROM | disk | FTP | other *** search
- /* _READ.C --- p. 650 */
- #include <stdio.h>
- #include <fcntl.h>
- #include <io.h>
- main()
- {
- char fname[40], *p_fname;
- char buffer[80];
- int filehandle, bytes_read;
- printf("Enter name of an existing file: ");
- p_fname = gets(fname);
- /* Open the file using _open */
- if((filehandle = _open(p_fname, O_RDONLY)) == -1)
- {
- printf("Error opening file: %s\n", fname);
- exit(0);
- }
- printf("File %s opened.\n", fname);
- /* Now read the first 80 bytes */
- if((bytes_read = _read(filehandle, buffer, 80)) != -1)
- {
- printf("%d bytes read\n", bytes_read);
- printf("The bytes read are :\n%s\n", buffer);
- }
- else
- printf("Error reading from file: %s\n", fname);
- /* Now close the file */
- if(_close(filehandle) != 0)
- {
- printf("Error closing file with _close\n");
- exit(0);
- }
- printf("File %s closed.\n", fname);
- }